﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PB.Enums;
using PB.Base;
using System.Text.RegularExpressions;
using System.IO;
using System.Xml.Linq;
using System.IO.IsolatedStorage;

namespace PB.Helpers
{
    /// <summary>
    /// Class with some methos utilities for logger
    /// </summary>
    public static class Utilities
    {
        /// <summary>
        /// Gets DateTime from a string date-time format (file xml)
        /// </summary>
        /// <param name="value">String of date-time</param>
        /// <returns>DateTime from xml string date-time</returns>
        public static DateTime GetXmlDateTime(string value)
        {
            string[] parts = value.Split('-');

            string[] date = parts.GetValue(0).ToString().Split('/');
            string[] time = parts.GetValue(1).ToString().Split(':');

            int day = Convert.ToInt32(date.GetValue(0));
            int month = Convert.ToInt32(date.GetValue(1));
            int year = Convert.ToInt32(date.GetValue(2));

            int hour = Convert.ToInt32(time.GetValue(0));
            int minute = Convert.ToInt32(time.GetValue(1));
            int second = Convert.ToInt32(time.GetValue(2));

            DateTime dt = new DateTime(year, month, day, hour, minute, second);

            return dt;
        }

        /// <summary>
        /// An extension method to gets a date-time string from DateTime
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns>Date-time string from DateTime</returns>
        public static string GetStringXml(this DateTime dateTime)
        {
            return String.Format("{0}-{1}", dateTime.ToShortDateString(), dateTime.ToLongTimeString());
        }

        internal class EnumLogs
        {
            public EnumLogs(LogType logType)
            {
                string selEnum = Enum.GetName(typeof(LogType), logType);

                string element = selEnum;
                string elements = selEnum == "Info" ? elements = "Informations" : elements = selEnum + "s"; // aggiunge una 's' mette il plurale

                this.NameElement = element;
                this.NameElements = elements;
            }

            public string NameElement { get; private set; }
            public string NameElements { get; private set; }
        }

        internal static User GetUserLog(XElement xelement)
        {
            var userAttr = xelement.Attribute("User");

            User user = null;

            if (userAttr != null)
            {
                user = XmlHelper.Users.GetUsers().Where(u => u.Username == userAttr.Value).SingleOrDefault();
            }

            return user;
        }

        /// <summary>
        /// An extension method to check if the DateTime specified has these values: 01/01/0001 and 00:00:00
        /// </summary>
        /// <returns>True if DateTime has thoose values</returns>
        public static bool IsNull(this DateTime dt)
        {
            if (dt.Year == 1 && dt.Day == 1 && dt.Month == 1 &&
                dt.Hour == 0 && dt.Minute == 0 && dt.Second == 0)
            {
                return true;
            }

            return false;
        }

        internal static string CheckFilename(this string path)
        {
            Regex fnReg = new Regex(@"^(([a-zA-Z]:)|(\))(\{1}|((\{1})[^\]([^/:*?<>""|]*))+)$");

            string filename = Path.GetFileName(path);

            if (!String.IsNullOrWhiteSpace(filename))
            {
                throw new ArgumentException("In the path, the filename is required.");
            }
            else
            {
                if (!fnReg.IsMatch(filename))
                {
                    throw new ArgumentException("This filename isn't valid.");
                }
            }

            return path;
        }

        internal static string CheckedPathXml(string path)
        {
            string extension = Path.GetExtension(path);

            if (String.IsNullOrWhiteSpace(extension))
                throw new ArgumentException("This 'xml' extension is required.");

            else if (extension != ".xml")
                throw new ArgumentException("The extension isn't valid. It must be 'xml'.");

            return path;
        }

        internal static IsolatedStorageFile Storage = IsolatedStorageFile.GetUserStoreForApplication();
    }
}
